package com.docker.sys.dic;

import com.docker.core.mvc.service.BaseService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Okv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import kit.docker.convert.RecordKit;

import java.util.List;
import java.util.Map;

public class DicService extends BaseService {

    @Inject
    private DicDao dicDao;

    public List<Record> all(Okv cond) {
        return dicDao.all(cond);
    }

    public boolean cache() {
        List<Record> cacheList = dicDao.cacheList();
        DicRedis.del();
        for (Record dic : cacheList) {
            DicRedis.save(dic);
        }
        return true;
    }

    public boolean save(DicBean dicBean, String userId) {
        if (dicDao.codeExist(Okv.by("dic_code", dicBean.getDic_code()).set("dic_pcode", dicBean.getDic_pcode()).set("dic_id", "-1"))) {
            throwError("字典编码已存在");
        }
        if (nameExist(dicBean.getDic_name(), dicBean.getDic_pcode(), "-1")) {
            throwError("字典名称已存在");
        }

        dicBean.setDic_id(getUUID32());
        dicBean.setStatus_flag("1");
        dicBean.setCreate_time(getNowDateTime());
        dicBean.setCreate_oper_id(userId);
        dicBean.setUpdate_time(dicBean.getCreate_time());
        dicBean.setUpdate_oper_id(userId);
        Record saveDic = RecordKit.objectToRecord(dicBean);
        if (!dicDao.save(saveDic)) {
            throwError("字典添加失败");
        }
        if (StrKit.notBlank(dicBean.getDic_pcode())) {
            DicRedis.save(saveDic);
        }
        return true;
    }

    private boolean nameExist(String dicName, String dicPcode, String dicId) {
        Okv cond = Okv.create()
                .set("dic_name", dicName)
                .set("dic_pcode", dicPcode)
                .set("dic_id", dicId);
        return dicDao.nameExist(cond).getInt("count") > 0;
    }

    public boolean update(DicBean dicBean, String userId) {
        Record dic = info(dicBean.getDic_id());
        if (dicDao.codeExist(Okv.by("dic_code", dicBean.getDic_code()).set("dic_pcode", dicBean.getDic_pcode()).set("dic_id", dicBean.getDic_id()))) {
            throwError("字典编码已存在");
        }
        if (nameExist(dicBean.getDic_name(), dicBean.getDic_pcode(), dicBean.getDic_id())) {
            throwError("字典名称已存在");
        }
        if (StrKit.isBlank(dic.getStr("dic_pcode"))) {
            updateChild(dicBean.getDic_code(), dic.getStr("dic_code"));
        }
        dic.set("dic_code", dicBean.getDic_code());
        dic.set("dic_name", dicBean.getDic_name());
        dic.set("dic_desc", dicBean.getDic_desc());
        return update(dic, userId);
    }

    private void updateChild(String newCode, String dicPcode) {
        dicDao.updateChild(newCode, dicPcode);
    }

    private boolean update(Record dic, String userId) {
        dic.set("update_time", getNowDateTime());
        dic.set("update_oper_id", userId);
        if (!dicDao.update(dic)) {
            throwError("字典修改失败");
        }
        return true;
    }

    public boolean update(String dicId, String statusFlag, String userId) {
        Record dic = info(dicId);
        dic.set("status_flag", statusFlag);
        update(dic, userId);

        if (StrKit.notBlank(dic.getStr("dic_pcode"))) {
            if ("1".equals(statusFlag)) {
                DicRedis.save(dic);
            } else {
                DicRedis.hDel(dic.getStr("dic_pcode"), dic.getStr("dic_code"));
            }
        }
        return true;
    }

    public Record info(String dicId) {
        Record dic = dicDao.info(dicId);
        if (!StrKit.notNull(dic)) {
            throwError("字典不存在");
        }
        return dic;
    }

    public Map<String, String> child(String dicPcode) {
        return DicRedis.child(dicPcode);
    }

}
